package com.rogue.member.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.rogue.common.core.utils.ResultDTO;
import com.rogue.member.domain.HsMember;
import com.rogue.member.domain.dto.MemberDTO;
import com.rogue.member.domain.dto.RegisterDTO;
import com.rogue.member.domain.vo.MemberVO;
import com.rogue.member.service.HsMemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @Author YeahLiang
 * @Create 2022/03/18
 */

@RestController
@RequiredArgsConstructor
@RequestMapping("/member")
public class MemberController {

    @Resource
    private final HsMemberService memberService;

    @Resource
    private final RestTemplate restTemplate;

    @Resource
    private final RedisTemplate redisTemplate;

    @Value("${oauth.client_id}")
    private String client_id;
    @Value("${oauth.client_secret}")
    private String client_secret;
    @Value("${oauth.grant_type}")
    private String grant_type;

    @RequestMapping("/easyCreateMember")
    public ResultDTO<Boolean> easyCreateMember(@RequestBody HsMember memberDTO) {
        return ResultDTO.success(memberService.create(memberDTO));
    }

    @RequestMapping("/easyDeleteMember/{id}")
    public ResultDTO<Boolean> easyDeleteMember(@PathVariable Long id) {
        return ResultDTO.success(memberService.removeById(id));
    }

    @RequestMapping("/easyUpdateMember")
    public ResultDTO<Boolean> easyUpdateMember(@RequestBody HsMember memberDTO) {
        return ResultDTO.success(memberService.updateById(memberDTO));
    }

    @RequestMapping("/easyGetOneMember/{id}")
    public ResultDTO<HsMember> easyGetOneMember(@PathVariable Long id) {
        return ResultDTO.success(memberService.getById(id));
    }

    @RequestMapping("/listMember")
    public ResultDTO<PageInfo<MemberVO>> listMember(@RequestBody MemberDTO memberDTO){
        return ResultDTO.success(memberService.listMember(memberDTO));
    }

    @RequestMapping("/updateStatus/{id}")
    public ResultDTO<Boolean> updateStatus(@PathVariable("id") Long id, Integer status){
        return ResultDTO.success(memberService.updateStatus(id,status));
    }

    @RequestMapping("/register")
    public ResultDTO register(@RequestBody RegisterDTO registerDTO){
        final String code = (String) redisTemplate.opsForValue().get(registerDTO.getPhone());
        if (Objects.equals(code,registerDTO.getSmsCode())) {
            // 注册成功
            return ResultDTO.success(memberService.create(BeanUtil.copyProperties(registerDTO,HsMember.class)));
        }
        return ResultDTO.success("注册失败！");
    }

    @RequestMapping("/getCaptcha")
    public ResultDTO getCaptcha(@RequestParam String phone){
        return ResultDTO.success(memberService.getCaptcha(phone));
    }
}
